<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use fast\Date;
use think\Db;

/**
 * APP背景图片管理
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示APP背景图片管理等
 */
class Background extends Backend
{
    protected $model = null;
    protected $noNeedRight = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\common\model\Background');
        // $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
        // $this->view->assign("categoryList", \app\common\model\Attachment::getCategoryList());
        // $this->assignconfig("categoryList", \app\common\model\Attachment::getCategoryList());
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $this->token();

            // 更新
            $params = $this->request->post("row/a");
            
            if ($params) {
                $params = $this->preExcludeFields($params);

                try {
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }

        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 查看
     */
    // public function index()
    // {
    //     try {
    //         \think\Db::execute("SET @@sql_mode='';");
    //     } catch (\Exception $e) {

    //     }
    //     $column = [];
    //     $starttime = Date::unixtime('day', -6);
    //     $endtime = Date::unixtime('day', 0, 'end');
    //     $joinlist = Db("user")->where('jointime', 'between time', [$starttime, $endtime])
    //         ->field('jointime, status, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(jointime), "%Y-%m-%d") AS join_date')
    //         ->group('join_date')
    //         ->select();
    //     for ($time = $starttime; $time <= $endtime;) {
    //         $column[] = date("Y-m-d", $time);
    //         $time += 86400;
    //     }
    //     $userlist = array_fill_keys($column, 0);
    //     foreach ($joinlist as $k => $v) {
    //         $userlist[$v['join_date']] = $v['nums'];
    //     }

    //     $dbTableList = Db::query("SHOW TABLE STATUS");
    //     $addonList = get_addon_list();
    //     $totalworkingaddon = 0;
    //     $totaladdon = count($addonList);
    //     foreach ($addonList as $index => $item) {
    //         if ($item['state']) {
    //             $totalworkingaddon += 1;
    //         }
    //     }
    //     $this->view->assign([
    //         'totaluser'         => User::count(),
    //         'totaladdon'        => $totaladdon,
    //         'totaladmin'        => Admin::count(),
    //         'totalcategory'     => \app\common\model\Category::count(),
    //         'todayusersignup'   => User::whereTime('jointime', 'today')->count(),
    //         'todayuserlogin'    => User::whereTime('logintime', 'today')->count(),
    //         'sevendau'          => User::whereTime('jointime|logintime|prevtime', '-7 days')->count(),
    //         'thirtydau'         => User::whereTime('jointime|logintime|prevtime', '-30 days')->count(),
    //         'threednu'          => User::whereTime('jointime', '-3 days')->count(),
    //         'sevendnu'          => User::whereTime('jointime', '-7 days')->count(),
    //         'dbtablenums'       => count($dbTableList),
    //         'dbsize'            => array_sum(array_map(function ($item) {
    //             return $item['Data_length'] + $item['Index_length'];
    //         }, $dbTableList)),
    //         'totalworkingaddon' => $totalworkingaddon,
    //         'attachmentnums'    => Attachment::count(),
    //         'attachmentsize'    => Attachment::sum('filesize'),
    //         'picturenums'       => Attachment::where('mimetype', 'like', 'image/%')->count(),
    //         'picturesize'       => Attachment::where('mimetype', 'like', 'image/%')->sum('filesize'),
    //     ]);

    //     $this->assignconfig('column', array_keys($userlist));
    //     $this->assignconfig('userdata', array_values($userlist));

    //     return $this->view->fetch();
    // }

}
